// eslint-disable-next-line import/named
import { MutationTree } from 'vuex'
import { ContentStateInterface } from './state'

const mutation: MutationTree<ContentStateInterface> = {
  setMenuList (state, payload) {
    state.menu.list = payload
    state.menu.isLoaded = true
  },
  setContentList (state, payload) {
    state.content.list = payload
  },
  addContentVote (state, content) {
    const id = content._id
    content.statistics.votes++
    state.contentVote.push(id)
  },
  popContentVote (state, content) {
    const id = content._id
    content.statistics.votes--
    state.contentVote.splice(id, 1)
  },
  pushContentCollection (state, content) {
    const id = content._id
    content.statistics.collectionCount++
    state.contentCollection.push(id)
  },
  deleteContentCollection (state, content) {
    const id = content._id
    content.statistics.collectionCount--
    state.contentCollection = state.contentCollection.filter(e => e !== id)
  },
  setCurrentContent (state, payload) {
    state.currentContent = {
      ...payload,
      isVote: false,
      isCollection: false
    }
  },
  syncKmFromUser (state, payload: {
    contentVote: string[],
    contentCollection: string[],
  }) {
    const {
      contentVote,
      contentCollection
    } = payload || {}
    if (contentVote) {
      state.contentVote = contentVote
    }
    if (contentCollection) {
      state.contentCollection = contentCollection
    }
    // console.log(list)
  }
}
export default mutation
